package com.legendframework.core.cmd.paramer.converter.impl;

import com.legendframework.core.ioc.IParameter;
import com.legendframework.core.ioc.annotation.Component;
import com.legendframework.core.cmd.annotation.Size;
import com.legendframework.core.cmd.exception.ParameterLimitException;
import com.legendframework.core.cmd.paramer.converter.ParamerConverter;
import org.bukkit.command.CommandSender;

@Component
public class StringParamerConverter implements ParamerConverter<String> {

    @Override
    public boolean check(Class<?> cls) {
        return String.class == cls;
    }

    @Override
    public String castParameter(CommandSender sender, String message, Class<String> type, IParameter parameter) {
        Size size = parameter.getAnnotation(Size.class);
        if (size == null) {
            return message;
        }
        if (message.length() < size.min() || message.length() > size.max()) {
            throw new ParameterLimitException(size.message());
        }
        return message;
    }
}
